import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/css/index.css'
import './assets/css/iconfont.css'
import config from './assets/js/config'
import axios from 'axios'
import VueAxios from 'vue-axios'
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/chart/pie'
import 'echarts/lib/chart/bar'
import 'echarts/lib/chart/map'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/legend'
import 'echarts/lib/component/title'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import VueCalendarHeatmap from 'vue-calendar-heatmap'
import tagCloud from './components/tag-cloud'
import dayjs from 'dayjs'

Vue.config.productionTip = false
Vue.prototype.config = config
Vue.use(mavonEditor)
Vue.use(ElementUI)
Vue.use(tagCloud)
Vue.use(VueCalendarHeatmap)
Vue.use(VueAxios, axios)
Vue.component('v-chart', ECharts)
Vue.prototype.$moment = dayjs

Vue.filter('date', function (value, formatStr = 'YYYY-MM-DD') {
    return dayjs(value).format(formatStr)
})

Vue.filter('dateTime', function (value, formatStr = 'YYYY-MM-DD HH:mm:ss') {
    return dayjs(value).format(formatStr)
})

NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.path === '/login') {
        next()
    } else if (!store.state.userInfo) {
        next({path: '/login'})
    } else {
        next()
    }
})

router.afterEach(() => {
    NProgress.done()
})

const baseUrls = {
    baidu: 'http://127.0.0.1:8080',
    fileDown: 'https://d.pcs.baidu.com/file',
    api: 'http://127.0.0.1:18084',
}

axios.interceptors.request.use((config) => {
    config.headers['liuJun-token'] = sessionStorage.getItem('token') === null ? '' : sessionStorage.getItem('token')
    /* /api/baidu/ /api  /baidu/ */
    if (config.url.indexOf('/api/') !== -1) {
        config['url'] = baseUrls.api + config.url.replace('/api', '')
    }
    console.log(config)

    return config
})

axios.interceptors.response.use(
    (response) => {
        switch (response.data.code) {
            case 50000:
                app.config.globalProperties.$notify({
                    title: 'Error',
                    message: '系统异常,请联系管理员',
                    type: 'error'
                })
                break
            case 40001:
                app.config.globalProperties.$notify({
                    title: 'Error',
                    message: '用户未登录',
                    type: 'error'
                })
                break
        }
        return response
    },
    (error) => {
        switch (error.response.data.code) {
            case 40001:
                app.config.globalProperties.$notify({
                    title: 'Error',
                    message: '用户未登录',
                    type: 'error'
                })
                break
        }
    }
)

new Vue({
    router,
    store,
    render: (h) => h(App)
}).$mount('#app')
